﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.IO;

namespace NuaTarDoneng.Tail
{
    /// <summary>
    /// Start 的摘要说明
    /// </summary>
    public class Start : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string result = "ER";
            string pubkey=File.ReadAllText(context.Server.MapPath("/Tail/pubkey.txt"));
            string action = context.Request.QueryString["action"];
            if (null != context.Session["uid"])
            {
                Logs.Log(context, context.Session["uid"].ToString() + ":" + action);
            }
            else
            {
                Logs.Log(context, action);
            }
            if (action == "getRandom"){

                if (null==context.Session["randomStr"] )
                {
                    result = Guid.NewGuid().ToString("N") ;
                    context.Session.Add("randomStr", result);
                    result = result + "#" + pubkey;
                    context.Session.Timeout = 2;
                }
                else
                {
                    result = context.Session["randomStr"].ToString() + "#"+pubkey;
                }

            }
            else if (action == "test")
            { 
            result="不是query："+context.Request["test"];
            }
            else if (action == "login" && null != context.Session["randomStr"])
            {
                string uid = context.Request.QueryString["uid"];
                string didApid = tailSql.getDidAPid(uid);
                string did = didApid.Split('#')[0];
                string pid = didApid.Split('#')[1];
                string pidq = context.Request.QueryString["pid"];
                string didq = context.Request.QueryString["did"];
                string pids = PubWays.EncryptToMD5(pid + context.Session["randomStr"]);
                string dids = PubWays.EncryptToMD5(did + context.Session["randomStr"]);
                if (pidq == pids && dids == didq)
                {
                    context.Session.Add("uid", uid);
                    if (null == context.Application["OnlineG"])
                    {
                        context.Application.Add("OnlineG", "#" + uid + "*");
                    }
                    else
                    {
                        string OnlineG = context.Application["OnlineG"].ToString();
                        context.Application.Lock();
                        context.Application["OnlineG"] = OnlineG + "#" + uid + "*";
                        context.Application.UnLock();
                    }

                    context.Application.Add("Status" + uid, "");
                    context.Application.Add("Message" + uid, "");
                    result = "OK";
                }


            }
            else if (action == "reg" && null != context.Session["randomStr"])
            {
                string randoms = SecurityMan.Decodes(pubkey, context.Request.QueryString["random"]);
                if (null != context.Session["randomStr"] && context.Session["randomStr"] == randoms)
                {
                    string usr = context.Request.QueryString["usr"];
                    string pid = SecurityMan.Decodes(pubkey, context.Request.QueryString["pid"]);
                    string did = SecurityMan.Decodes(pubkey, context.Request.QueryString["did"]);
                    string uid = Guid.NewGuid().ToString("N");
                    string code = SecurityMan.GenerKey();
                    result = tailSql.RegIt(usr, pid, did, uid, code);
                }

            }
            else if (action == "logout" && null != context.Session["randomStr"])
            {
                string usr = context.Request.QueryString["usr"];
                string usrs = SecurityMan.Encodes(pubkey, context.Session["uid"].ToString() + context.Session["randomStr"].ToString());
                if (usr == usrs)
                {
                    string mname = "Message" + context.Session["uid"];
                    string sname = "Status" + context.Session["uid"];
                    context.Application.Remove(mname);
                    context.Application.Remove(sname);
                    string OnlineG = context.Application["OnlineG"].ToString();
                    string uid = context.Session["uid"].ToString();
                    OnlineG = OnlineG.Replace("#" + uid + "*", "");
                    context.Application.Lock();
                    context.Application["OnlineG"] = OnlineG;
                    context.Application.UnLock();
                    context.Session.RemoveAll();
                    result = "OK";
                }
            }
            
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}